<?php

namespace app\service\tag;

use app\models\cacheModel\NewsCategoryModel as NewsCategoryCacheModel;
use app\models\tableModel\NewsCategoryModel as NewsCategoryDbModel;
use yii\base\Model;
use yii\helpers\ArrayHelper;

/**
 * 新闻分类 模板标签
 * Class NewsCategoryList
 * @package app\models
 */
class NewsCategory extends Model
{

    /**
     * 最终数据
     * @var
     */
    public $data;

    /**
     * 运行的方法 - 默认分类列表
     * @var bool
     */
    public $run = 'getList';

    /**
     * 分类编号
     * @var
     */
    public $id;

    /**
     * 父级编号
     * @var int
     */
    public $parentId = 0;

    /**
     * 是否加载子集
     * @var bool
     */
    public $loadChild = false;

    /**
     * 基础连接
     *  ` 当[url_type]为站内连接时，将有相关逻辑
     * @var
     */
    public $baseUrl = '/newsCate/{id}.html';

    /**
     * 移除的编号列表
     * @var
     */
    public $removeIdList;

    /**
     * 展示规定字段
     * @var array
     */
    public $fields = [
        'id',
        'name',
        'parent_id',
        'level',
        'status',
        'description',
        'url_type',
        'url',
        'extra_data',
        'is_show',
        'description'
    ];

    public function init()
    {
        parent::init();
        $this->run();
    }

    public function beforeRun()
    {

    }

    public function afterRun()
    {

    }

    /**
     * 运行总方法
     */
    public function run()
    {

        ### 需要执行的方法未找到
        if (!$this->run || !method_exists($this, $this->run)) return false;

        $function = $this->run;
        $this->data = $this->$function();
        return $this->data;
    }

    /**
     * 获取分类的列表
     * @return array|\yii\redis\ActiveRecord[]
     */
    public function getList() {

        ### 查询列表
        // 父级别[model]
        $model = NewsCategoryCacheModel::loadModel();
        // 格式化条件 - 移除的编号

        ### 条件塑造
        $condition = [];
        // 父级编号
        if (isset($this->parentId)) $condition[] = ['IN', 'parent_id', $this->parentId];
        // 过滤编号
        $this->removeIdList = !is_array($this->removeIdList) ?
            explode(',', $this->removeIdList) : $this->removeIdList;
        if (isset($this->removeIdList)) $condition[] = ['NOT IN', 'id', $this->removeIdList];

        ### 执行查询
        $this->data = $model->loadWhere(ArrayHelper::merge([
            'AND',
            ['IN', 'status', NewsCategoryDbModel::getStatusOpen()],
            ['IN', 'is_show', NewsCategoryDbModel::getIsShowShow()],
        ], $condition))->getPaginate(1, 999999, [
            'loadChild' => $this->loadChild,
            'baseUrl' => $this->baseUrl
        ])->getList();

        return $this->data;
    }

    /**
     * 分类当前位置列表
     * @param bool $id 分类编号
     * @return mixed
     */
    public function getPosition($id = false) {
        // 编号
        $id = $id === false ? $this->id : $id;
        // 分类详情
        $detail = NewsCategoryCacheModel::loadModel($id);
        if (!$detail || $detail['status'] != NewsCategoryDbModel::getStatusOpen()) {
            return $this->data;
        }
        $detail = $detail->getAttributes();

        // 数据叠加
        $this->data = ArrayHelper::merge([
            [
                'id' => $detail['id'],
                'name' => $detail['name'],
                'url' => $detail['url_type'] == NewsCategoryDbModel::getUrlTypeOutside() ?
                    $detail['url'] : str_replace(['{id}'], [$detail['id']], $this->baseUrl)
            ]
        ], !$this->data ? [] : $this->data);

        // 上级编号为0结束返回
        if ($detail['parent_id'] == 0) return $this->data;
        // 否则继续向上查找
        else return $this->getPosition($detail['parent_id']);
    }
}
